<template>
	<view class="view-root">
		<view class="status-notice">页面跳转中...</view>
	</view>
</template>

<script>
import {
	token,imInfo, doctorInfo, currentImInfo
} from "@/utils/token.js"

	const app = getApp();
	export default {
		data() {
			return {
				orderCode: '',
				destoryTime: null
			};
		},
		onLoad(options) {
			// 二维码参数约定：
			// 	1.scene--使用场景
			// 		患者邀请：'user_invite'
			// 		医生邀请：'doctor_invite'
			// 		机构邀请：'agency_invite'
			// 		订单核销：'order_verify'
			//		药品分享：'medicine_share'
			//  2.id--订单的id/code或者邀请用户的id
			console.log(options);
			switch (options.scene) {
				case 'user_invite':
					clearInterval(this.destoryTime);
					getApp().globalData.parentUserId = options.id;
					if (token.get()) {
						uni.reLaunch({
							url: '/pages/index/index'
						})
					} else {
						uni.redirectTo({
							url:'/pages-mall/login/login'
						})
					}
					break;
				case 'doctor_invite':
					getApp().globalData.parentDoctorId = options.id;
					// 借用切端逻辑
					this.identitySwitch(1);
					break;
				case 'agency_invite':
					// 借用切端逻辑
					getApp().globalData.parentAgencyId = options.id;
					this.identitySwitch(3);
					break;
				case 'order_verify':
					this.orderCode = options.id;
					// 传4是为了切机构同时不跳转到机构首页
					this.identitySwitch(4);
					break;
				case 'medicine_share':
					uni.redirectTo({
						url: '/pages-mall/medicineDetail/medicineDetail?id=' + options.id
					});
					break;
			}
		},
		onUnload() {
			clearInterval(this.destoryTime);
		},
		methods: {
			//切换角色
			identitySwitch(roleType) {
				this.$api
					.identitySwitch({
						params: {
							role: roleType == 4 ? 3: roleType
						}
					})
					.then(res => {
						if (roleType == 3 || roleType == 4) {
							if (res.code == 200) {
								this.getAgencyInfo(roleType);
							} else {
								this.$api.getAgenciesState().then(e => {
									const { code, result } = e
									if (code == 200) {
										if (result.code == -1) {
											//跳转申请页面
											uni.redirectTo({
												url:'/pages-agencies/settled/settled'
											})
										}
										if(result.code == 0){
											uni.redirectTo({
												url:'/pages-agencies/auditDuring/auditDuring'
											})
										}
										if(result.code == 2){
											uni.redirectTo({
												url:'/pages-agencies/auditRefused/auditRefused'
											})
										}
									}
								})
							}
						} else {
							if (res.code == 200) {
								this.getDoctorInfo(roleType);
							} else {
								//跳转申请页面
								uni.redirectTo({
									url:'/pages-doctor/auth/auth?doctorType=' + roleType
								})
							}
						}
					});
			},
			
			confirmSwitch(roleType, sdkAppId, id, userSig) {
				if (roleType == 0) {
					app.globalData.role = 0;
					currentImInfo.set({
						sdkAppId: imInfo.get().sdkAppId,
						id: imInfo.get().id,
						userSig: imInfo.get().userSig
					}, Date.parse(new Date()) + (86400000 * 29));
					getApp().globalData.txyImController.logout();
					getApp().globalData.txyImController.login(imInfo.get().id, imInfo.get().userSig);
					uni.reLaunch({
						url: '/pages/index/index'
					});
				} else if (roleType == 1) {
					app.globalData.role = 1;
					currentImInfo.set({
						sdkAppId,
						id,
						userSig
					}, Date.parse(new Date()) + (86400000 * 29));
					getApp().globalData.txyImController.logout();
					getApp().globalData.txyImController.login(id, userSig);
					uni.reLaunch({
						url: '/pages/doctor/doctor'
					});
				} else if (roleType == 2) {
					app.globalData.role = 2;
					currentImInfo.set({
						sdkAppId,
						id,
						userSig
					}, Date.parse(new Date()) + (86400000 * 29));
					getApp().globalData.txyImController.logout();
					getApp().globalData.txyImController.login(id, userSig);
					uni.reLaunch({
						url: '/pages-doctor/pharmacist/pharmacist'
					});
				} else if (roleType == 3) {
					app.globalData.role = 3;
					// currentImInfo.set({
					// 	sdkAppId,
					// 	id,
					// 	userSig
					// }, Date.parse(new Date()) + (86400000 * 29));
					// getApp().globalData.txyImController.logout();
					// getApp().globalData.txyImController.login(id, userSig);
					uni.reLaunch({
						url: '/pages-agencies/agencies/agencies'
					});
				} else {
					app.globalData.role = 3;
					// 此处是当roleType == 4时的特殊处理
					uni.redirectTo({
						url: '/pages-order/orderDetail/orderDetail?code=' + this.orderCode + '&forVerify=1'
					})
				}
			},
			
			//获取医生/药师个人信息
			getDoctorInfo(roleType) {
				this.$api.getDoctorInfo({}).then(res => {
					if (roleType != 0 && res.code == 200 && res.result != null) {
						// this.confirmSwitch(res.result?.imToken, res.result?.id);
						doctorInfo.set(res.result, Date.parse(new Date()) + (86400000 * 29))
						// 获取医生/药师的腾讯云IM信息
						this.$api.doctorTxyImInfo({
							params: {
								id: res.result.id
							}
						}).then(res_ => {
							this.confirmSwitch(roleType, res_.result.sdkAppId, res_.result.id, res_.result.userSig);	
						})
					} else if (roleType == 0) {
						this.confirmSwitch(0);
					} else {
						uni.showToast({
							title: '身份切换失败',
							icon: 'none'
						});
					}
				});
			},
			
			getAgencyInfo(role) {
				this.confirmSwitch(role);
			},
		}
	}
</script>

<style lang="less" scoped>
@import './qrcodeHandler.less';
</style>
